package Controllers;
import Information.UserInfo;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.io.*;
import java.net.URL;
import java.util.ResourceBundle;

public class FansController extends Application implements Initializable {

    int SetStage = 0;   //旗帜，用以表明用户点击的按钮
    UserInfo userInfo;

    @FXML
    private AnchorPane PaneFollow;      //关注界面

    @FXML
    private AnchorPane PaneFans;        //粉丝界面

    @FXML
    private VBox VboxFans;

    @FXML
    private VBox VboxFollow;



    private void getdate() {
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/UserPosts/currentUser.ser"))) {
            try {
                userInfo = (UserInfo) ois.readObject();
            } catch (ClassNotFoundException e) {
                System.out.println(4);
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < userInfo.getFansID().size(); i++) {
            //针对粉丝id建立对应的Borderpane并插入vbox
            VboxFans.getChildren().addAll(CreateFans(userInfo.getFansID().get(i)), new Separator());
        }

        for (int i = 0; i < userInfo.getFollowID().size(); i++) {
            //针对关注id建立对应的Borderpane并插入vbox
            VboxFollow.getChildren().addAll(CreateFollow(userInfo.getFollowID().get(i)), new Separator());
        }
    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            AnchorPane root = FXMLLoader.load(getClass().getResource("../Resources/Fans.fxml"));
            Scene scene = new Scene(root, 700.0, 700.0);
            primaryStage.getIcons().add(new Image("/Images/logoblue.png"));
            primaryStage.setTitle("粉丝界面");
            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        PaneFollow.setVisible(false);
        PaneFans.setVisible(true);
        getdate();
    }


    @FXML
    void ChangeFollow(ActionEvent event) {
        PaneFollow.setVisible(true);
        PaneFans.setVisible(false);
    }

    @FXML
    void ChangeSpecial(ActionEvent event) {
        String info = "该功能尚未实现";
        Alert alert = new Alert(Alert.AlertType.INFORMATION, info, new ButtonType("确定", ButtonBar.ButtonData.YES));
        alert.setHeaderText(null);
        alert.setTitle("提示");
        alert.show();
    }

    @FXML
    void ChangeFans(ActionEvent event) {
        PaneFollow.setVisible(false);
        PaneFans.setVisible(true);
    }

    public BorderPane CreateFans(String FansID) {
        //建立对应粉丝id的borderpane
        BorderPane borderPane = new BorderPane();
        UserInfo userInfo_Fans = null;
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/UserPosts/userObjects/" + FansID + ".ser"))) {
            try {
                userInfo_Fans = (UserInfo) ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            //设置并插入粉丝头像
            assert userInfo_Fans != null;
            Image image = new Image(userInfo_Fans.getAvatar());
            ImageView Picture = new ImageView(image);
            Picture.setFitHeight(80.0);
            Picture.setFitWidth(80.0);

            //设置并插入粉丝昵称
            Label pname = new Label(userInfo_Fans.getPname());
            pname.setFont(Font.font("System", 23));
            pname.setTranslateY(30);
            pname.setTranslateX(10);

            HBox a = new HBox();
            a.getChildren().addAll(Picture,pname);
            borderPane.setLeft(a);

        } catch (IOException e) {
            e.printStackTrace();
        }
        borderPane.setPrefHeight(100.0);
        return borderPane;
    }

    private BorderPane CreateFollow(String FollowsID) {
        //建立对应关注id的borderpane
        BorderPane borderPane = new BorderPane();
        UserInfo userInfo_Follow = null;
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/UserPosts/userObjects/" + FollowsID + ".ser"))) {
            try {
                userInfo_Follow = (UserInfo) ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }


            //设置并插入关注值的头像和昵称
            assert userInfo_Follow != null;
            Image image = new Image(userInfo_Follow.getAvatar());
            ImageView Postpicture = new ImageView(image);
            Postpicture.setFitHeight(80.0);
            Postpicture.setFitWidth(80.0);      //设置头像的偏移程度

            Label pname = new Label(userInfo_Follow.getPname());
            pname.setFont(Font.font("System", 23));
            pname.setTranslateY(30);
            pname.setTranslateX(10);            //设置昵称的偏移程度

            HBox a = new HBox();
            a.getChildren().addAll(Postpicture,pname);
            borderPane.setLeft(a);


            //建立取消关注按钮
            Button DeleteFollow = new Button("取消关注");
            UserInfo finalUserInfo = userInfo_Follow;
            DeleteFollow.setOnAction(event -> {
                if(DeleteFollow.getText().equals("取消关注")){
                    DeleteFollow.setText("关注");
                    finalUserInfo.getFollowID().remove(FollowsID);

                }else{
                    DeleteFollow.setText("取消关注");
                    finalUserInfo.getFollowID().add(FollowsID);
                }
                try(
                        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/UserPosts/userObjects/"+ finalUserInfo.getId()+".ser")))
                {   //修改用户的userinfo中的关注数组
                    oos.writeObject(finalUserInfo);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try(
                        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/UserPosts/currentUser.ser")))
                {   //修改用户的userinfo中的关注数组
                    oos.writeObject(finalUserInfo);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String info = "修改成功";
                Alert alert = new Alert(Alert.AlertType.INFORMATION, info, new ButtonType("确定", ButtonBar.ButtonData.YES));
                alert.setHeaderText(null);
                alert.setTitle("提示");
                alert.show();
            });
            DeleteFollow.setTranslateX(-20);
            DeleteFollow.setTranslateY(30);
            borderPane.setRight(DeleteFollow);
        } catch (IOException e) {
            e.printStackTrace();
        }
        borderPane.setPrefHeight(100.0);

        return borderPane;
    }

}
